const mongoose = require("mongoose");
const { Schema } = mongoose;

// 用户模型
const userSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
  },
  password: {
    type: String,
    required: true,
  },
  nickname: {
    type: String,
    required: true,
  },
  avatar: {
    type: String,
    required: true,
  },
});
const DemoUser = mongoose.model("DemoUser", userSchema);

// 好友列表模型
const friendSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "DemoUser",
    required: true,
  },
  friendId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "DemoUser",
    required: true,
  },
  friendNickname: {
    type: String,
    required: true,
  },
  friendAvatar: {
    type: String,
    default: "",
  },
});
const DemoFriendList = mongoose.model("DemoFriendList", friendSchema);

// 聊天记录模型
const chatRecordSchema = new mongoose.Schema({
  _id: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    auto: true,
  },
  content: {
    type: String,
    required: true,
  },
  timestamp: {
    type: Date,
    required: true,
    default: Date.now,
  },
  senderId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "DemoUser",
    required: true,
  },
  receiverId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "DemoUser",
    required: true,
  },
});
const DemoChatRecord = mongoose.model("DemoChatRecord", chatRecordSchema);

module.exports = {
  DemoUser,
  DemoFriendList,
  DemoChatRecord,
};
